﻿// Author:					HungNM
// Created:					27-07-2012
// Last Modified:			27-07-2012
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;

namespace eProject.Business
{
    public class CssHelper
    {
        public CssHelper()
        {

        }

        private bool includeJqueryUICSS;
        private bool includeStyleCSS;
        private bool includeSmoothMenuCSS;
        private bool includeLayoutCSS;

        public bool IncludeLayoutCSS
        {
            get { return includeLayoutCSS; }
            set
            {
                includeLayoutCSS = value;
                if (includeLayoutCSS) { SetupIncludeLayoutCSS(); }
            }
        }

        public bool IncludeJqueryUICSS
        {
            get { return includeJqueryUICSS; }
            set
            {
                includeJqueryUICSS = value;
                if (includeJqueryUICSS) { SetupIncludeJqueryUICSS(); }
            }
        }

        public bool IncludeStyleCSS
        {
            get { return includeStyleCSS; }
            set
            {
                includeStyleCSS = value;
                if (includeStyleCSS) { SetupIncludeStyleCSS(); }
            }
        }

        public bool IncludeSmoothMenuCSS
        {
            get { return includeSmoothMenuCSS; }
            set
            {
                includeSmoothMenuCSS = value;
                if (includeSmoothMenuCSS) { SetupIncludeSmoothMenuCSS(); }
            }
        }


        private void SetupIncludeJqueryUICSS()
        {
            if (includeJqueryUICSS)
            {
                AddCssFile("jquery-ui-1.8.23.custom.css", "/js/css/", null);
            }
        }

        private void SetupIncludeStyleCSS()
        {
            if (includeStyleCSS)
            {
                AddCssFile("style.css", "/Css/", "screen");
            }
        }

        private void SetupIncludeSmoothMenuCSS()
        {
            if (includeSmoothMenuCSS)
            {
                AddCssFile("ddsmoothmenu.css", "/Css/", "screen");
            }
        }

        private void SetupIncludeLayoutCSS()
        {
            if (includeLayoutCSS)
            {
                AddCssFile("layout.css", "/Css/", null);
            }
        }

        public static void AddCssFile(string File, string Directory, string media)
        {
            System.Web.UI.Page CurrentPage = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
            bool Added = false;

            foreach (Control CurrentControl in CurrentPage.Header.Controls)
            {
                if (CurrentControl.GetType() == typeof(HtmlLink))
                {
                    HtmlLink HtmlLink = (HtmlLink)CurrentControl;
                    if (HtmlLink.Attributes["href"] != null)
                    {
                        if (HtmlLink.Attributes["href"].Contains(File))
                        {
                            Added = true;
                        }
                    }
                }
            }

            if (!Added)
            {
                HtmlLink CssControl = new HtmlLink();
                CssControl.Attributes.Add("type", "text/css");
                CssControl.Attributes.Add("rel", "stylesheet");
                CssControl.Attributes.Add("href", Directory + File);
                if (string.IsNullOrEmpty(media))
                {
                    media = "all";
                }
                CssControl.Attributes.Add("media", media);
                CurrentPage.Header.Controls.Add(CssControl);
            }
        }
    }
}
